package com.yykj.drup.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yykj.drup.server.mapper.DoctorStoreDao;
import com.yykj.drup.server.model.DoctorStore;
import com.yykj.drup.server.service.DoctorStoreService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zengg
 * @since 2022-03-10
 */
@Service
@AllArgsConstructor
public class DoctorStoreServiceImp extends ServiceImpl<DoctorStoreDao, DoctorStore> implements DoctorStoreService {

    final DoctorStoreDao doctorStoreDao;

    @Override
    public boolean insertDoctor(String doctocId, Integer[] ids) {
        List<DoctorStore> doctorStores = new ArrayList<>();
        Integer sortValue = this.findMaxSortValue(doctocId);
        if(ids.length != 0 ){
            for (int i = 0; i < ids.length; i++) {
                DoctorStore doctorStore = this.findByDoctorIdAndStoreId(doctocId, ids[i]);
                if (doctorStore == null){
                    doctorStore = new DoctorStore(doctocId,ids[i]);
                    doctorStore.setSort(++sortValue);
                    doctorStores.add(doctorStore);
                }
            }
        }
        return this.saveBatch(doctorStores);
    }

    @Override
    public Integer doctorRemoveStore(String doctorId, Integer storeId) {
        QueryWrapper<DoctorStore> wrapper = new QueryWrapper<>();
        wrapper
                .eq("doctor_id",doctorId)
                .eq("store_id",storeId);
        return doctorStoreDao.delete(wrapper);
    }

    @Override
    public DoctorStore findByDoctorIdAndStoreId(String doctorId, Integer storeId) {
        QueryWrapper<DoctorStore> wrapper = new QueryWrapper<>();
        wrapper
                .eq("doctor_id",doctorId)
                .eq("store_id",storeId);
        return doctorStoreDao.selectOne(wrapper);
    }

    @Override
    public Integer findMaxSortValue(String doctocId) {
        Integer sortValue = 0;
        QueryWrapper<DoctorStore> wrapper = new QueryWrapper<>();
        wrapper
                .eq("doctor_id",doctocId)
                .orderByDesc("sort").last("limit 1");
        DoctorStore doctorStore = doctorStoreDao.selectOne(wrapper);
        return doctorStore == null ? sortValue : doctorStore.getSort();
    }

    @Override
    public List<Integer> findStoreIdsSortByDoctocId(String doctocId) {
        List<Integer> storeIds = new ArrayList<>();
        QueryWrapper<DoctorStore> wrapper = new QueryWrapper<>();
        wrapper
                .eq("doctor_id",doctocId)
                .orderByDesc("sort");
        List<DoctorStore> doctorStores = doctorStoreDao.selectList(wrapper);
        if (!doctorStores.isEmpty()){
            storeIds = doctorStores.stream().map(i -> i.getStoreId()).collect(Collectors.toList());
        }
        return storeIds;
    }
}
